#include <iostream>
#include <unistd.h>
using namespace std;


typedef void(*work_p)(int);
typedef void(*task_p)();

void printLog()
{
    cout << "printLog()" << endl;
}

void loadConf()
{
    cout << "loadConf()" << endl;
}

void connectMysql()
{
    cout << "connectMysql()" << endl;
}
                                                                                                                                                       
task_p tasks[] = {printLog, loadConf, connectMysql};

int randTask()
{
    return rand() % 3;                                                                                                                                 
}

void work(int rfd)
{
    while(true) //一直循环读取管道里在内容
    {
        int taskcode = 0;
        int n = read(rfd, &taskcode, sizeof(taskcode)); //如果管道中没有数据，子进程会阻塞在read
        if(n == sizeof(taskcode)) //判断读取是否正常
        {
            if(taskcode >= 3) //判断读取是否合法
                continue;
            tasks[taskcode](); //执行任务
        }
        else if(n == 0) //读到0的时候结束
        {
            cout << "subprocess " << getpid() << " quit now" << endl;
            break;
        }
        cout << "i am worker:" << getpid() << endl;
        sleep(1);
    }
}